package com.zmj.sy.mom.srv.aps.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.sy.mom.srv.aps.bean.entity.mes.WmsPlatePick;
import com.zmj.sy.mom.srv.aps.bean.vo.plate.PlatePickListResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.report.ReportPickListResVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface WmsPlatePickMapper extends SyMapper<WmsPlatePick> {

    @Select(" select pick.id, plate.mater_code, plate.mater_name, plate.nesting_drawing, plate.texture, plate.norm, plate.`status`, plate.pick_user_id, plate.pick_user_name, plate.pick_date_time, plate.nest_date_time nestDateTimeStr" +
            " from tb_mes_plate_pick pick " +
            " left join tb_mes_plate plate on pick.material_id = plate.mater_code " +
            " ${ew.customSqlSegment} ")
    Page<PlatePickListResVo> list(Page<PlatePickListResVo> page, @Param(Constants.WRAPPER) QueryWrapper<WmsPlatePick> query);

    @Select("select max(picking_list_no) from tb_mes_plate_pick")
    Integer selectMax();

    @Select(" select pick.id, pick.pick_id, pick.material_id plateCode, pick.picking_list_no pickingListNo, pick.plate_state status, pick.storage_place storeCode, " +
            " nest_plate.texture spec, nest_plate.norm thickness, nest_plate.sequence seq, nest_plate.length, nest_plate.width, nest_plate.heat_number heatNumber, nest_plate.png_url pngUrl, " +
            " plate.work_chain workChain, awd.group_code chainGroupCode, task.station_code stationCode, " +
            " nest_excess.ex_materiel_id excessCode, " +
            " nest_nest.nest_id nestId, nest_nest.need_date planStartDateTime ," +
            " awd.actual_start_date_time actualStartDateTime, awd.actual_end_date_time actualFinishDateTime, " +
            " plate.status plateStatus " +
            " from tb_mes_plate_pick pick " +
            " left join tb_third_sinocam_receive_nest_plate nest_plate on pick.material_id = nest_plate.stock_list_no " +
            " left join tb_third_sinocam_receive_nest_excess nest_excess on pick.material_id = nest_excess.stock_list_no " +
            " left join tb_third_sinocam_receive_nest_nest nest_nest on nest_nest.receive_nest_id = nest_plate.receive_nest_id " +
            " left join tb_mes_plate plate on plate.mater_code = pick.material_id " +
            " left join tb_aps_work_detail awd on awd.mater_no = pick.material_id and awd.phase_code in ( 'JG', 'HG') " +
            " LEFT JOIN tb_mes_work_detail_task task on task.mater_code =  pick.material_id  AND task.phase_code IN ( 'JG', 'HG' ) AND task.type = 1" +
            " ${ew.customSqlSegment} ")
    List<ReportPickListResVo> pickList(@Param(Constants.WRAPPER) QueryWrapper<WmsPlatePick> query);

    @Select(" select sum(move.total_cut_time) from tb_mes_plate_pick pp  " +
            " left join tb_third_sinocam_receive_nest_plate rp on pp.material_id = rp.stock_list_no  " +
            " left join tb_third_sinocam_receive_nest_nest rn on rp.receive_nest_id = rn.receive_nest_id " +
            " left join tb_third_sinocam_receive_nest_move move on move.receive_nest_id = rp.receive_nest_id and move.move_type = 1 " +
            " ${ew.customSqlSegment} ")
    Integer cutTime(@Param(Constants.WRAPPER) QueryWrapper<Object> query);

    @Select("DELETE FROM tb_mes_plate_pick WHERE id = #{id}")
    void deleteMesNest(@Param("id") Integer id);

    @Select("DELETE FROM tb_mes_plate_pick WHERE material_code = #{plateCode}")
    void deleteMesNestWithCode(@Param("plateCode") String plateCode);

}
